<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\AppUsers;
use App\Models\Config;
use App\Models\Records;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class LoginController extends Controller
{
    public function login(Request $request)
    {
        if (!$request->post('username', '')) {
            return $this->errors('-1', '请输入用户名');
        }

        if (!$request->post('password', '')) {
            return $this->errors('-1', '请输入密码');
        }

        if (!$request->post('uuid', '')) {
            return $this->errors('-1', '缺少设备编号');
        }

        if ($user = AppUsers::with('level')->where('email', $request->post('username'))->first()) {
            if (Hash::check($request->post('password', ''), $user->password)) {
                if ($user->uuid !== $request->post('uuid', '')) {
                    $user->api_token = Str::random(60);
                    $user->uuid      = $request->post('uuid', '');
                    $user->save();
                }
                $expires        = Carbon::parse($user->expires);
                $now            = Carbon::now();
                $user->minutes  = !$expires->greaterThan($now) ? 0 : $now->diffInMinutes($expires);
                $config         = Config::first();
                $user->open     = $config ? $config->is_switch : false;
                $user->level_id = 0;
                $user           = $user->toArray();
                $user['level']  = [
                    'id'     => 0,
                    'name'   => '普通用户',
                    'weight' => 0,
                ];
                return $this->success(200, '登陆成功', $user);
            }
        }

        return $this->errors('-1', '账号或密码错误');
    }

    public function register(Request $request)
    {
        if (!$request->post('username', '')) {
            return $this->errors('-1', '请输入用户名');
        }

        if (!$request->post('password', '')) {
            return $this->errors('-1', '请输入密码');
        }

        if (!$request->post('password_confirmation')) {
            return $this->errors('-1', '请输入确认密码');
        }

        if ($request->post('password', '') !== $request->post('password_confirmation')) {
            return $this->errors('-1', '两次密码不一致');
        }

        if (!$request->post('uuid', '')) {
            return $this->errors('-1', '缺少设备编号');
        }

        if (AppUsers::whereEmail($request->post('username'))->first()) {
            return $this->errors('-1', '用户名已存在');
        }
        $data          = $request->all();
        $data['email'] = $data['username'];
        unset($data['username']);
        $data['password']   = bcrypt($data['password']);
        $data['name']       = 'vn_' . Str::random(6);
        $data['api_token']  = Str::random(60);
        $data['code']       = Str::random(6);
        $records            = Records::where('uuid', $data['uuid'])->first();
        $data['start_time'] = Carbon::now()->format('Y-m-d H:i:s');
        $data['expires']    = $records != null ? Carbon::now()->format('Y-m-d H:i:s') : Carbon::now()->addDay(1)->format('Y-m-d H:i:s');
        if ($user = AppUsers::create($data)) {
            if ($records == null) {
                $records          = new Records();
                $records->user_id = $user->id;
                $records->uuid    = $user->uuid;
                $records->save();
            }
            $expires       = Carbon::parse($user->expires);
            $now           = Carbon::now();
            $user->minutes = !$expires->greaterThan($now) ? 0 : $now->diffInMinutes($expires);
            $config        = Config::first();
            $user->open    = $config ? $config->is_switch : false;
            return $this->success(200, '注册成功', $user->toArray());
        }
        return $this->errors('-1', '注册失败');
    }
}
